package org.kl.bf.web.basic;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.kl.bf.entity.basic.Menu;
import org.kl.bf.entity.easyui.EasyuiUtils;
import org.kl.bf.service.basic.MenuService;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springside.modules.web.MediaTypes;


/**
 * 菜单管理
 * 
 * @author Qken
 * @date 2014-3-24
 */
@Controller
@RequestMapping(value = "/basic/menu")
public class MenuController extends BasicController {

	@Autowired
	private MenuService menuService;
	
	@RequiresPermissions("menu:list")
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "basic/menuList";
	}

	@RequiresPermissions("menu:list")
	@RequestMapping(value = "/treeGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String treeGrid() {
		String result = jsonMapper.toJson(menuService.treeGrid(false));
		return result;
	}
	@RequiresPermissions("menu:list")
	@RequestMapping(value = "/treeGridWithFunctions", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String treeGridWithFunctions() {
		String result = jsonMapper.toJson(menuService.treeGrid(true));
		return result;
	}

	/**
	 * 获取整个菜单树
	 * 
	 * @return
	 */
	@RequestMapping(value = "/tree", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String tree() {
		String result = jsonMapper.toJson(menuService.tree(""));
		return result;
	}

	/**
	 * 通过Url获取单个菜单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/menuByUrl", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String menuByUrl(@RequestParam(value = "menuUrl", defaultValue = "") String menuUrl) {
		Menu menu = menuService.findByMenuUrl(menuUrl);
		if (menu == null) {
			return "{}";
		}
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String result = jsonMapper.toJson(EasyuiUtils.buildMenuTree(menu, request.getContextPath()));
		return result;
	}

	@RequiresPermissions("menu:update")
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String update(@Valid @ModelAttribute("menu") Menu menu) {
		try {
			menuService.saveMenu(menu);
			return ajaxMsg("保存成功", true, null);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("保存失败", false, null);
		}
	}

	@RequiresPermissions("menu:delete")
	@RequestMapping(value = "delete/{id}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String delete(@PathVariable("id") String id) {
		String msg = "";
		try {
			msg = menuService.multiDelete(id);
			return ajaxMsg("删除成功"+ msg, true, null);
		} catch (Exception e) {
			return ajaxMsg("删除失败"+ e.getMessage(), false, null);
		}
	}
	
	
	@RequiresPermissions({ "menu:delete" })
	@RequestMapping(value = "multiDelete", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String multiDelete(@RequestParam(value = "ids", defaultValue = "") String ids) {
		//String msg = "";
		try {
			 menuService.multiDelete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("删除失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("删除成功！" , true, null);
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void getMenu(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("menu", menuService.getMenu(id));
		}
	}
	@RequestMapping(value = "drop", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String drop(@RequestParam(value = "id", defaultValue = "") String id,@RequestParam(value = "pid", defaultValue = "") String pid,@RequestParam(value = "rootid", defaultValue = "") String rootid) {
		Menu menu = menuService.getMenu(id);
		menu.setMenuParentId(pid);
		if(rootid.equals("-1")){
			menu.setRootId(pid);
		}else{
			menu.setRootId(rootid);
		}
		try {
			menuService.saveMenu(menu);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("移动失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("移动成功！" , true, null);
	}
	
	@RequestMapping(value = "drop1", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String drop1(@RequestParam(value = "id", defaultValue = "") String id,@RequestParam(value = "pid", defaultValue = "") String pid,
			@RequestParam(value = "rootid", defaultValue = "") String rootid,@RequestParam(value = "displayOrder", defaultValue = "") Integer displayOrder) {
		Menu menu = menuService.getMenu(id);
		menu.setMenuParentId(pid);
		menu.setDisplayOrder(displayOrder);
		if(rootid.equals("-1")){
			menu.setRootId(pid);
		}else{
			menu.setRootId(rootid);
		}
		try {
			menuService.saveMenu(menu);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("移动失败:" + e.getMessage(), false, null);
		}
		return ajaxMsg("移动成功！" , true, null);
	}
}
